package com.gz.moreaseManager.modules.service.impl;

import com.gz.moreaseManager.core.base.Constants;
import com.gz.moreaseManager.modules.entity.Brand;
import com.gz.moreaseManager.modules.entity.Information;
import com.gz.moreaseManager.modules.mapper.InformationMapper;
import com.gz.moreaseManager.modules.service.IInformationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gz.moreaseManager.modules.transfer.InforVo;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 资讯信息 服务实现类
 * </p>
 *
 * @author wike
 * @since 2019-02-26
 */
@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information> implements IInformationService {


    @Resource
    private InformationMapper informationMapper;


    /**
     * 获取时尚资讯列表
     * @return
     */
    public List<Information> findInforFashion(){
        return findInformationByAuthCode(Constants.INFOR_TYPE_FASHION);
    }

    /**
     * 获取品牌资讯列表
     * @return
     */
    public List<Information> findInforBrand(){
        return findInformationByAuthCode(Constants.INFOR_TYPE_BRAND);
    }

    @Override
    public InforVo findInforVo(Integer id,Integer type) {
        if( UtilTool.isNotBlank( id )){
            InforVo inforVo = new InforVo();
            inforVo.setId( id );
            inforVo.setType( type );
            Information information = informationMapper.selectById( id );
            if( UtilTool.isNotBlank( information )){
                inforVo.setInformation( information );

                List<Information> inforList = null;
                Map<Integer,Information> inforMap = new HashMap<Integer,Information>();
                if( type == 1){//时尚资讯
                    inforList = findInforFashion();
                    inforVo.setBackUrl("/web/fashionInfor");
                }else if( type == 2 ){//品牌资讯
                    inforList = findInforBrand();
                    inforVo.setBackUrl("/web/brandInfor");
                }
                if( UtilTool.isNotBlank( inforList )){
                    int index = 1,current =1,prev=1,next =inforList.size();
                    for( Information information1 : inforList ){
                        if( information1.getId() == id ){
                            current = index;
                        }
                        inforMap.put(index,information1);
                        index++;
                    }
                    //获取上一条和下一条
                    if( current -1 >0)
                        prev = current -1;
                    if( current + 1 < inforList.size() )
                        next = current +1;

                    if( UtilTool.isNotBlank(inforMap.get(prev ))){
                        Information prevData = inforMap.get(prev );
                        inforVo.setPrevId( prevData.getId() );
                        inforVo.setPrevName( prevData.getTitle());
                    }

                    if( UtilTool.isNotBlank(inforMap.get( next  ))){
                        Information nextData = inforMap.get( next );
                        inforVo.setNextId( nextData.getId() );
                        inforVo.setNextName( nextData.getTitle() );
                    }

                }

                return inforVo;
            }

        }

        return null;
    }

    /**
     * 根据权限码获取信息
     * @param authCode
     * @return
     */
    private List<Information> findInformationByAuthCode(String authCode ){

        if( UtilTool.isNotBlank( authCode )){
            List<Information> list= informationMapper.findInformationByAuthCode( authCode );
            if( UtilTool.isNotBlank( list ) && list.size() > 0)
                return list;
        }
        return null;
    }
}
